#include "wxsresource.h"
#include "wxsextresmanager.h"
#include "wxsresourcetreeitemdata.h"
#include "M_Code_auibook.h"
#include <editormanager.h>

namespace {
  const int EditOpenId = wxNewId();
  const int EditCloseId = wxNewId();
  const int DeleteId = wxNewId();
}

class wxsResource::wxsResourceRootTreeItemData: public wxsResourceTreeItemData {
  public:
    wxsResourceRootTreeItemData( wxsResource* Resource ):
      wxsResourceTreeItemData(),
      m_Resource( Resource ) {
    }
  private:
    virtual void OnSelect() {
      m_Resource->EditOpen();
    }
    virtual void OnRightClick() {
      wxMenu Menu;
      if( m_Resource->GetEditor() ) {
        Menu.Append( EditCloseId, _T( "Close editor" ) );
      } else {
        Menu.Append( EditOpenId, _T( "Open editor" ) );
      }
      Menu.Append( DeleteId, _T( "Delete this resource" ) );
      m_Resource->OnFillPopupMenu( &Menu );
      PopupMenu( &Menu );
    }

    virtual bool OnPopup( long Id ) {
      if( Id == EditOpenId ) {
        m_Resource->EditOpen();
        return true;
      }
      if( Id == EditCloseId ) {
        m_Resource->EditClose();
        return true;
      }
      if( Id == DeleteId ) {
        DeleteResource();
        return true;
      }
      return m_Resource->OnPopupMenu( Id );
    }

    void DeleteResource() {
      if( m_Resource->DeleteCleanup( true ) ) {
        m_Resource->GetProject()->DelResource( m_Resource );
      }
    }

    wxsResource* m_Resource;
};

wxsResource::wxsResource( wxsProject* Owner, const wxString& ResourceType, const wxString& GUI ):
  m_ResourceType( ResourceType ),
  m_ResourceName( wxEmptyString ),
  m_GUI( GUI ),
  m_Owner( Owner ),
  M_Code_Editor( 0 ),
  m_Language( wxsCPP ) {}

wxsResource::~wxsResource() {
  if( M_Code_Editor ) {
    wxsEditor* EditorStore = M_Code_Editor;
    M_Code_Editor = 0;
    EditorStore->Close();
  }
  if( m_TreeItemId.IsOk() ) {
    wxsTree()->Delete( m_TreeItemId );
  }
}

void wxsResource::EditOpen() {
  if( M_Code_Editor ) {
    M_Code_Editor->Activate();
  } else {
    wxsResourceTree::Get()->BlockSelect();
    M_Code_Editor = OnCreateEditor( Manager::Get()->GetEditorManager()->GetNotebook() );
    wxsResourceTree::Get()->UnblockSelect();
  }
}

void wxsResource::EditClose() {
  if( M_Code_Editor ) {
    M_Code_Editor->Close();
  }
}

void wxsResource::EditorClosed() {
  M_Code_Editor = 0;
  if( !m_Owner ) {
    wxsExtRes()->EditorClosed( this );
  }
}

void wxsResource::BuildTreeEntry( const wxsResourceItemId& Parent ) {
  m_TreeItemId = wxsTree()->AppendItem( Parent, GetResourceName(), OnGetTreeIcon(), OnGetTreeIcon(),
                                        new wxsResourceRootTreeItemData( this ) );
}

bool wxsResource::ReadConfig( const TiXmlElement* Node ) {
  m_ResourceName = cbC2U( Node->Attribute( "name" ) );
  m_Language = wxsCodeMarks::Id( cbC2U( Node->Attribute( "language" ) ) );
  if( GetResourceName().empty() ) {
    return false;
  }
  return OnReadConfig( Node );
}

bool wxsResource::WriteConfig( TiXmlElement* Node ) {
  bool Result = OnWriteConfig( Node );
  Node->SetAttribute( "name", cbU2C( m_ResourceName ) );
  Node->SetAttribute( "language", cbU2C( wxsCodeMarks::Name( m_Language ) ) );
  return Result;
}

IMPLEMENT_CLASS( wxsResource, wxObject )
